import http from "@/api";
import { ResPage, User, OrgManager } from "@/api/interface/index";
import qs from "qs";
/**
 * @name 服务级别模块
 */

/**
 * @name 查询服务级别
 */
// 查询服务级别
export const FindSlaContractManagePage = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.postitsm<string>(`/pages/slaContractManage!find.action`, qs.stringify(newobj));
};

// 获取用户部门列表
export const getUserDepartmentList = (params: OrgManager.ReqOrgManagerParams) => {
  params.page = params.pageNum;
  params.rows = params.pageSize;
  params["organizationQueryDto.orgName"] = params.orgName == undefined ? "" : params.orgName;
  return http.get(`/pages/organization!find.action?companyNo=1`, {}, { params });
};

// 查询sla
export const findSlaById = (params: string) => {
  return http.postitsm<string>(`/pages/slaContractManage!findSlaById.action?contractNo=` + params, qs.stringify({}));
};

// 查询被服务机构
export const findByOrgServices = (params: string) => {
  return http.postitsm<string>(`/pages/slaContractManage!findByOrgServices.action?contractNo=` + params, qs.stringify({}));
};

// 查询相关服务列表
export const findByServiceDir = (params: string) => {
  return http.postitsm<string>(`/pages/slaContractManage!findByServiceDir.action?contractNo=` + params, qs.stringify({}));
};

// 查询sla
export const findSlaRule = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.postitsm<string>(`/pages/slaRule!find.action`, qs.stringify(newobj));
};

// 判断SLA是否已经存在
export const isSlaNameExist = (params: string) => {
  return http.postitsm<string>(`/pages/slaRule!slaNameExist.action?slaRuleQueryDTO.ruleName=` + params, qs.stringify({}));
};

// 保存规则
export const saveSlaRule = (params: {}, header: string, condition: any[]) => {
  let newobj = {};
  let name = "";
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "contractNo") {
        newobj[header + ".slaContract." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "patternBinding") {
        newobj[header + ".condition." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "patternType") {
        newobj[header + ".condition." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "rulePatternNo") {
        newobj[header + ".condition." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "rulePackageNo") {
        newobj[header + ".rulePackage." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "ruleName") {
        name = `${params[key]}`;
        newobj[header + "." + `${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  console.log("sdafdafddddddddddddddddddddddddddd");
  console.log(condition);
  console.log(condition.length);
  if (condition.length > 0) {
    let num = 0;
    for (let i = 0; i < condition.length; i++) {
      let item = condition[i];
      Object.keys(item).forEach(key => {
        if (`${key}` === "sequence") {
          newobj[header + ".condition.constraints[" + num.toString() + "]." + `${key}`] = num + 1;
        } else {
          newobj[header + ".condition.constraints[" + num.toString() + "]." + `${key}`] = `${item[key]}`;
        }
      });
      num++;
      console.log("5555555555555555555555555555555555555");
    }
    // condition.forEach(item=>{
    //   console.log("5555555555555555555555555555555555555")
    //  Object.keys(item).forEach(key => {
    //    newobj[header + ".condition.constraints["+num.toString()+"]."+`${key}` ] =  `${item[key]}`;
    //  });
    //  num++
    // })
  }
  newobj[header + ".actions[0].propertyName"] = `matchRuleName`;
  newobj[header + ".actions[0].dataType"] = `String`;
  newobj[header + ".actions[0].sequence"] = `1`;
  newobj[header + ".actions[0].givenValue"] = name;
  console.log("qs.stringify(newobj)");
  console.log(qs.stringify(newobj));
  // return http.post<ResPage<User.ResUserList>>("" + `/pages/itsopUser!findITSOPUserPager.action`, {}, { params });
  return http.postitsm<string>(`/pages/slaRule!save.action`, qs.stringify(newobj));
};

// 编辑规则
export const mergeSlaRule = (params: {}, header: string, condition: any[]) => {
  let newobj = {};
  let name = "";
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "contractNo") {
        newobj[header + ".slaContract." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "patternBinding") {
        newobj[header + ".condition." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "patternType") {
        newobj[header + ".condition." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "rulePatternNo") {
        newobj[header + ".condition." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "rulePackageNo") {
        newobj[header + ".rulePackage." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "ruleName") {
        name = `${params[key]}`;
        newobj[header + "." + `${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  console.log("sdafdafddddddddddddddddddddddddddd");
  console.log(condition);
  console.log(condition.length);
  if (condition.length > 0) {
    let num = 0;
    for (let i = 0; i < condition.length; i++) {
      let item = condition[i];
      Object.keys(item).forEach(key => {
        if (`${key}` === "sequence") {
          newobj[header + ".condition.constraints[" + num.toString() + "]." + `${key}`] = num + 1;
        } else {
          newobj[header + ".condition.constraints[" + num.toString() + "]." + `${key}`] = `${item[key]}`;
        }
      });
      num++;
    }
    // condition.forEach(item=>{
    //   console.log("5555555555555555555555555555555555555")
    //  Object.keys(item).forEach(key => {
    //    newobj[header + ".condition.constraints["+num.toString()+"]."+`${key}` ] =  `${item[key]}`;
    //  });
    //  num++
    // })
  }
  newobj[header + ".actions[0].propertyName"] = `matchRuleName`;
  newobj[header + ".actions[0].dataType"] = `String`;
  newobj[header + ".actions[0].sequence"] = `1`;
  newobj[header + ".actions[0].givenValue"] = name;
  console.log("qs.stringify(newobj)");
  console.log(qs.stringify(newobj));
  return http.postitsm<string>(`/pages/slaRule!merge.action`, qs.stringify(newobj));
};

// 查询服务规则
export const findSLARuleByContractNoAndRuleNo = (contractNo: string, ruleNo: string) => {
  return http.postitsm<string>(
    `/pages/slaRule!findSLARule.action?contractNo=` + contractNo + `&ruleNo=` + ruleNo,
    qs.stringify({})
  );
};

// 获取流程
export const findPageProcessDefinitions = () => {
  return http.get(`/pages/jbpm!findPageProcessDefinitions.action?rows=10000`, {}, { cancel: false });
};

// 获取资产分类
export const getConfigurationCategoryTreeAllData = () => {
  return http.get(
    `/pages/ciCategory!getConfigurationCategoryTreeAllData.action?_=1721111822508&parentEventId=0`,
    {},
    { cancel: false }
  );
};

// 删除规则
export const deleteSlaRule = (params: string) => {
  return http.post<String>(`/pages/slaRule!delete.action` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 查询SLA逾期
export const findPromoteRulePage = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.postitsm<string>(`/pages/promoteRule!findPromoteRulePage.action`, qs.stringify(newobj));
};

// 保存SLA逾期
export const savePromoteRule = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.post(`/pages/promoteRule!savePromoteRule.action`, qs.stringify(newobj));
};

// 编辑SLA逾期
export const mergePromoteRule = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.post(`/pages/promoteRule!mergePromoteRule.action`, qs.stringify(newobj));
};

// 删除SLA逾期
export const deletePromoteRules = (params: string) => {
  return http.post<String>(`/pages/promoteRule!deletePromoteRules.action` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 删除SLA
export const deleteSlaContractManage = (params: string) => {
  return http.post<String>(`/pages/slaContractManage!delete.action` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 保存SLA
export const saveSlaContractManage = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.post(`/pages/slaContractManage!save.action`, qs.stringify(newobj));
};

// 编辑SLA
export const editSlaContractManage = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.post(`/pages/slaContractManage!merge.action`, qs.stringify(newobj));
};
